home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7853 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.2 KB

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is &Variable (declared as: char Variable[10])?
  5. Date: 27 Feb 1996 11:09:11 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4gvksnINNnug@anvil.ugrad.cs.ubc.ca>
  8. References: <4gqpa1$3h9@alcor.usc.edu> <4gsdno$1bg@umbc9.umbc.edu> <4gtab6$acb@ceylon.gte.com>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10.  
  11. In article <4gtab6$acb@ceylon.gte.com>, Brenda  <g051286> wrote:
  12. >schlein@umbc.edu (Jonas J. Schlein) wrote:
  13. >>Abu Wawda <wawda@alcor.usc.edu> wrote:
  14. >>|> I'm having trouble understanding what the address of a static array
  15. >>|> is.
  16. >>
  17. >>I'm having trouble understanding why it matters? You almost never use the
  18. >>address of an array directly unless doing something tricky with pointers
  19. >>or with particular dimensions of a multiple dimensional array.
  20. >>
  21. >>|> For example, if I declare a variable called myarray as:
  22. >>|>     char myarray[10];
  23. >>|> then what could &myarray possibly mean? myarray is not a pointer, so
  24. >>|> &myarray could not possibly be the address of the variable myarray
  25. >>|> (like it would be if I did char* myarray and then asked for &myarray).
  26. >>
  27. >>Yes it could and yes it is...'myarray' is not a pointer, but &myarray is
  28. >>a pointer to 'myarray'.
  29. >
  30. >Um, that's not correct.  myarray is DEFINITELY a pointer!  As declared above,
  31.  
  32. False. The _name_ 'myarray' is an array object. The _expression_ 'myarray'
  33. produces a pointer.
  34.  
  35. >it is a constant pointer to 10 contiguous char datatypes.  myarray is an
  36.  
  37. It is not a constant pointer. I don't see a 'const' keyword anywhere, and it
  38. is not a pointer to begin with. You can't make 'myarray' point somewhere else
  39. by initialization. True const pointers can be _initialized_ to point
  40. to an arbitrary object (of compatible type), either through a parameter pass,
  41. static initialization or automatic initialization.
  42.  
  43. Secondly, the type of the expression 'myarray' is not a pointer to 10
  44. datatypes. It is a pointer to one character. A 'char *'. A pointer to 10
  45. contiguous characters is 'char (*)[10]'.
  46.  
  47. >ADDRESS whereas *(myarray + 5) or myarray[5] is the 6th element in the array.
  48. >The difference between an array and something like "char *p=myarray", is that
  49. >you can say p++, but you can't say myarray++.  You shouldn't say &myarray
  50. >either because myarray is a constant, but I read that on some compilers
  51. >scanf ignores the dereferencing and does not bother to warn you.
  52.  
  53. This is false. ANSI gives meaning to applying the & operator to an array.
  54. You are way off base. The result of applying & to an array is a pointer to the
  55. whole array. In this case, it is a char (*)[10]  or pointer to an array of ten
  56. characters.
  57.  
  58. This operation is important. It allows you to do this:
  59.  
  60.     typedef char des_cblock[8];
  61.  
  62. and thereafter treat "des_cblock" as a type which holds eight characters. You
  63. can have functions that take a "des_cblock *key" argument, and the compiler
  64. can typecheck that against calling with a wrong array type.
  65.  
  66. The above typedef is from a well known freeware DES encryption library, LibDES.
  67. It effectively hides the fact that a key is an array of eight blocks. It could
  68. easily be changed to a structure, and the client programs wouldn't know the
  69. difference, since the library calls take _pointers_ to des_cblock. If they did
  70. not, and you changed from arrays to structures, you would be changing the
  71. passing of a pointer to the first element of an array (effectively a pass of
  72. the array object by reference) to a pass by value of a structure, thus failing
  73. to preserve the semantics of the library calls.
  74.  
  75. I find such typedefs very useful. For example, in some programs I have certain
  76. character fields that appear in more than one structure type. For example, a
  77. customer identification code (say 10 characters) can appear in an invoice or in
  78. a customer record. If you do this:
  79.  
  80.     typedef char cust_code_t[10];
  81.  
  82. You can then declare structure members of type 'cust_code_t' in various
  83. structures, and use a pointers to 'cust_code_t' in functions that expect a
  84. customer ID parameter.
  85.  
  86. Instead of messy #defines for field sizes, I can just use
  87. sizeof(cust_code_t) to get the size, as I would if it were a struct, union or
  88. anything else. 
  89.  
  90.  
  91.  
  92.  
  93. pointer. If you use 
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102. -- 
  103.  
  104.